#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //QMenuBar* menuBar = new QMenuBar();   //这种写法会导致内存泄漏
    QMenuBar* menuBar = this->menuBar();   //不会导致内存泄漏
    this->setMenuBar(menuBar);

    QMenu* menu1 = new QMenu("文件(&F)");
    QMenu* menu2 = new QMenu("编辑(&E)");
    QMenu* menu3 = new QMenu("关于(&A)");

    QMenu* menuChild1 = new QMenu("子菜单1");
    QMenu* menuChild2 = new QMenu("子菜单1");

    menuBar->addMenu(menu1);
    menuBar->addMenu(menu2);
    menuBar->addMenu(menu3);

    QAction* act1 = new QAction("打开(&O)");
    QAction* act2 = new QAction("保存(&S)");
    QAction* act3 = new QAction("另存为(&X)");
    QAction* act4 = new QAction("关闭(&C)");

    menu1->addMenu(menuChild1);
    menu1->addAction(act1);
    menu1->addAction(act2);
    menu1->addAction(act3);
    menu1->addSeparator();
    menu1->addAction(act4);
    menu1->addMenu(menuChild2);

    connect(act1,&QAction::triggered,this,&MainWindow::handel1);
    connect(act2,&QAction::triggered,this,&MainWindow::handel2);
    connect(act3,&QAction::triggered,this,&MainWindow::handel3);
    connect(act4,&QAction::triggered,this,&MainWindow::close);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handel1()
{
    qDebug()<<"act1";
}

void MainWindow::handel2()
{
    qDebug()<<"act2";
}

void MainWindow::handel3()
{
    qDebug()<<"act3";
}

